package com.ios.controller;

import com.ios.dto.PsychologicalQuestionExecution;
import com.ios.dto.PsychologicalTestExecution;
import com.ios.dto.PsychologicalTestTypeExecution;
import com.ios.dto.PsychologyResultExecution;
import com.ios.enums.PsychologicalTestEnum;
import com.ios.service.impl.PsychologicalServiceImpl;
import com.ios.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/psychological",method = {RequestMethod.POST})
public class PsychologicalController {
    @Autowired
    private PsychologicalServiceImpl psychologicalService;

    @RequestMapping(value = "/showtypelist")
    @ResponseBody
    public Map<String,Object> ShowTypeList(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            PsychologicalTestTypeExecution pte = psychologicalService.ShowAllType();
            if (pte.getState()== PsychologicalTestEnum.SUCCESS_SHOWTYPE.getState()){
                modelMap.put("success",true);
                modelMap.put("TestTypeList",pte.getPsychologicalTestTypeList());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",pte.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "/showtestbytype")
    @ResponseBody
    public Map<String,Object> ShowTestByType(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String typename = HttpServletRequestUtil.getString(request,"typename");
        try {
            PsychologicalTestExecution pt = psychologicalService.ShowTestByType(typename);
            if (pt.getState()==PsychologicalTestEnum.SUCCESS_SHOWTEST.getState()){
                modelMap.put("success",true);
                modelMap.put("TestList",pt.getPsychologicalTestList());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",pt.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "showalltest")
    @ResponseBody
    public Map<String,Object> ShowAllTest(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            PsychologicalTestExecution pt = psychologicalService.ShowAllTest();
            if (pt.getState()==PsychologicalTestEnum.SUCCESS_SHOWTEST.getState()){
                modelMap.put("success",true);
                modelMap.put("TestList",pt.getPsychologicalTestList());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",pt.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "/question")
    @ResponseBody
    public Map<String,Object> Question(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        Integer test_id = HttpServletRequestUtil.getInt(request,"test_id");
        try {
            PsychologicalQuestionExecution pq = psychologicalService.ShowQuestion(test_id);
            if (pq.getState()==PsychologicalTestEnum.SUCCESS_SHOWQUESTION.getState()){
                modelMap.put("success",true);
                modelMap.put("QuestionList",pq.getPsychologicalTestQuestionList());
            }else {
                modelMap.put("success",false);
                modelMap.put("errMsg",pq.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }

    @RequestMapping(value = "/showresult")
    @ResponseBody
    public Map<String,Object> Result(HttpServletRequest request){
        Map<String, Object> modelMap = new HashMap<String, Object>();
        String answer = HttpServletRequestUtil.getString(request,"answer");
        int test_id = HttpServletRequestUtil.getInt(request,"test_id");
        try{
            PsychologyResultExecution pr = psychologicalService.TestResult(answer,test_id);
            if (pr.getState()==PsychologicalTestEnum.SUCCESS_RESULT.getState()){
                modelMap.put("success",true);
                modelMap.put("ResultStandard",pr.getPsychologicalTestResult());
                modelMap.put("TestScore",pr.getResultScore());
                modelMap.put("TestInfo",pr.getResultInfo());
            }else {
                modelMap.put("sucess",false);
                modelMap.put("errMsg",pr.getStateInfo());
            }
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }

}
